<template>
  <UI :form="form" :loading="loading" @send="sendCaptcha" @submit="login"></UI>
</template>

<script setup>
import UI from './ui/index.vue'

import { loginCaptchaApi } from '@/api/auth'
import { useAuthStore } from '@/stores/modules/auth'
import { useRouter } from 'vue-router'

import axios from 'axios'
const authStore = useAuthStore()

const router = useRouter()

const form = ref({
  email: '',
  captcha: '',
  
})

const loading = ref(false)


const sendCaptcha = () => {
  const params = {
    email: form.value.email
  }
  loginCaptchaApi(params)
}

const login = async () => {
  loading.value = true
  try {
    const result = await authStore.login({
      email: form.value.email,
      captcha: form.value.captcha
    });
    
    if (result?.code === 200) {
      router.push({ name: 'conversation' });
    }
    
  } catch (error) {
    console.error('登录失败:', error);
    ElMessage.error(error.message || '登录失败');
  } finally {
    loading.value = false;
  }
}

</script>

<style lang="scss" scoped>
</style>
